<?php

/**

 * 订单管理

 * @author maniac

 */

namespace app\manage\controller;



use app\common\service\Wechat;
use app\facade\Orders AS OrdersFacade;

use app\common\model\Orders AS OrdersModel;

use app\facade\UserLogs;



class Orders extends Base

{

    /**
     * 首页列表
     */
    public function index()
    {
        // 当前页数
        $page   = input('get.page/d', 1);
        // 模型
        $model  = $this->getModel();
        // 查询条件
        $where  = $this->where();

        // 排序条件
        $order  = $this->order();
        // 数据总数
        $count  = $model::where($where)->count();
        //数据总金额
        $pay_amount  = $model::where($where)->sum('pay_amount');
        // 每页条数
        $limit  = $this->limit();
        // 分页条件
        $offset = $this->offset($page, $limit);
        // 分页数据
        $data   = $model::field(true)->where($where)->order($order)->limit($offset)->select();
        // 模板
        $template = $this->indexTemplate();

        // 列表附加
        $this->indexAttach($data);

        return $this->fetch($template, [
            'page'  => $page,
            'count' => $count,
            'limit' => $limit,
            'data'  => $data,
            'pay_amount'  => $pay_amount,
        ]);
    }



    /**

     * 详情页附加
     * @param array $info 查看的信息
     */
    public function addAttach($info)
    {
        
    }
    
    /**
     * 组合 where 查询条件
     */
    public function where()
    {
        $order_status_id = input('get.order_status_id/d', 0);
        $keywords        = input('get.keywords/s', '');
        $startTime       = input('get.start_time/s', '');
        $endTime         = input('get.end_time/s', '');

        $member_id         = input('get.member_id/s', '');

        $store         = input('get.store/s', '');

        $store_id         = input('get.store_id/s', '');
        $order_type  = input('order_type');


        $where           = [];
        if ($order_type == 1){
            $where[] = [
                ['order_type', 'eq', 1],
//                ['order_type', 'eq', 2],
            ];
        }
        if ($order_type == 3){
            $where[] = [
                ['order_type', 'eq', 3],
            ];
        }
        if ($order_status_id) {
            switch ($order_status_id) {
                case 1:
                    // 已取消
                    $where[] = [
                        ['order_status', 'eq', 2],
                    ];
                    break;
                case 2:
                    // 未付款
                    $where[] = [
                        ['order_status', 'eq', 1],
                        ['pay_status', 'eq', 0],
                    ];
                    break;
                case 3:
                    // 已付款
                    $where[] = [
                        ['order_status', 'eq', 1],
                        ['pay_status', 'eq', 1],
                    ];
                    break;
                case 4:
                    // 待发货
                    $where[] = [
                        ['order_status', 'eq', 1],
                        ['pay_status', 'eq', 1],
                        ['shipping_status', 'eq', 0],
                    ];
                    break;
                case 5:
                    // 待收货
                    $where[] = [
                        ['order_status', 'eq', 1],
                        ['pay_status', 'eq', 1],
                        ['shipping_status', 'eq', 1],
                    ];
                    break;
                case 6:
                    $where[] = [
                        ['order_status', 'eq', 255],
                    ];
                    // 已完成
                    break;
            }
        }



        if ($member_id > 0) {

            $where[] = [

                ['userid','eq',$member_id],

            ];

        }
        if ($store_id > 0) {

            $where[] = [

                ['store_id', 'eq',$store_id],

            ];

        }

        
        if ($startTime) {
            $where[] = [
                ['create_time', 'egt', strtotime($startTime)],
            ];
        }
        if ($endTime) {
            $where[] = [
                ['create_time', 'lt', strtotime($endTime)],
            ];
        }
        
        $this->assign('order_status_id', $order_status_id);
        $this->assign('keywords', $keywords);
        $this->assign('startTime', $startTime);
        $this->assign('endTime', $endTime);

        $this->assign('member_id', $member_id);

        $this->assign('store', $store);

        $this->assign('store_id', $store_id);


        return $where;
    }
    
    /**
     * 导出 excel
     * @return [type] [description]
     */
    public function export()
    {
        $where = $this->where();
        UserLogs::insertLog(['title' => '导出订单列表']);
        OrdersFacade::export($where);
    }
    
    /**
     * 指派配送员
     */
    public function allot()
    {
        $id     = input('post.id/d', 0);
        $userid = input('post.userid/d', 0);
        if (!$id || !$userid) {
            return j(1, '指派失败');
        }
        try {
            $result = OrdersFacade::allot($id, $userid);
            if ($result) {
                return j(0, '指派成功');
            }
            return j(1, '指派失败');
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }
    

    /**

     * 订单操作

     */

    public function orderAction()

    {

        $data = input('post.');
        //确认收款
        if(isset($data['pay_status']) &&$data['pay_status'] == 1){
            $data['pay_type'] = 8;
            $data['pay_time'] = time();
        }
        //确认发货
        if (isset($data['shipping_status']) && $data['shipping_status'] == 1) {
            //获取用户信息
            $order = OrdersModel::where("id",$data['id'])->field('id,order_no,pay_amount,userid')->find();
            $member = \app\common\model\Members::where("id",$order['userid'])->field('id,mobile,openid')->find();
            //统一服务消息
            $message = [
                'first'        => ['value' => '亲爱的用户'], //头
                'keyword1'     => ['value' => $member['mobile']], //手机号
                'keyword2'     => ['value' => $order['order_no']],  //订单号
                'keyword3'     => ['value' => $order['pay_amount']."积分"],  //金额
                'remark'       => ['value' => '亲，有一张订单已发货'], //尾
            ];
            $url = '/pages/index/index';
            //发送模板消息
            (new Wechat())->sendMessageAll($member['openid'],'ti7F_O_VECfoLEANslgVS5q8RQ-IAF3CXjbx_fi3txk',$message,'wxc88926430f17f6b2',$url);
        }
        //确认收货
        if (isset($data['shipping_status']) && $data['shipping_status'] == 2) {
            $data['order_status'] = 255;
        }
        OrdersModel::update($data);
        return j(0, '操作成功');

    }

}

